<template>
  <div class="goods">
    <!-- 面包屑区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索与添加商品区域 -->
      <el-row :gutter="48">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="params.query" clearable @clear="handleCurrentChange(1);getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="handleCurrentChange(1);getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4"><el-button type="primary" @click="goAddPage">添加商品</el-button></el-col>
      </el-row>

      <!-- 商品列表表单区域 -->
      <el-table :data="goodsList.goods" stripe border>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="120"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="180">
          <template v-slot="scope">
            {{ scope.row.add_time | dataformat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template v-slot="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteGood(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页列表功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="params.pagesize"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="goodsList.total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getGoodsList, deleteGoodsList } from 'network/goods_list'

export default {
  name: "Goods",
  data() {
    return {

      // 获取商品列表数据 请求参数
      params: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },

      // 商品列表数据
      goodsList: []
    }
  },
  created() {
    this.getGoodsList()
  },

  methods: {
    // 获取商品列表数据
    getGoodsList() {
      getGoodsList(this.params).then(res => {
        if(res.meta.status !== 200 ) return this.$message.error(res.meta.msg)
        this.goodsList = res.data
        // console.log(res);
    }).catch(err => this.$message.error(err))
    },

    // 分页 pageSize 发生改变
    handleSizeChange(newsize) {
      this.params.pagesize = newsize
      this.getGoodsList()
    },

    // 分页 currentPage 改变时会触发
    handleCurrentChange(newpage) {
      this.params.pagenum = newpage
      this.getGoodsList()
    },

    // 删除商品列表数据
    deleteGood(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteGoodsList(id).then(res => {
          if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
          this.$message.success(res.meta.msg)
          this.getGoodsList()
        }).catch(err => this.$message.error(err))
      }).catch(err => this.$message.error(err))
    },

    // 点击添加商品
    goAddPage() {
      this.$router.replace('goods/add')
    }
  }
}
</script>

<style>
  .el-card {
    margin-top: 20px;
  }

  .el-row {
    margin-bottom: 20px;
  }
</style>